#include "./models.h"

#include "qlib/all.h"

#include "../select/select_cmd.hpp"

#include "../axis/axis.hpp"

#include "../layer/layer.hpp"

namespace qbe::ol {

Models::Models(ecs::Query* _query) : query(_query) {}

void Models::clearAllSelect() {
  auto cell = query->get_resource<qlib::Cell::s_ptr>();
  auto& reg = cell->reg;

  auto& select_cmd = query->get_resource<select::SelectCmd>();
  select_cmd.clear_all();
}

qlib::Vec2d Models::getAbsorb() {
  auto& axis_models = query->get_resource<axis::Axis>();
  qlib::Vec2d pos = qlib::Vec2d(axis_models.real_x, axis_models.real_y);

  auto& select_cmd = query->get_resource<select::SelectCmd>();
  qlib::Vec2d res_p;
  qlib::GeoPartType res_type;

  bool have_absorb = select_cmd.get_adsorb(pos.x, pos.y, res_p, res_type);

  if (have_absorb) {
    return res_p;
  } else {
    return pos;
  }
}

bool Models::currentLayerIsHide(uint32_t layer) {
  auto& layerModels = query->get_resource<layer::Manager>();

  qlib::Tag tag = {layer, 0};

  return layerModels.is_hide(tag);
}

}  // namespace qbe::ol